/*
 * Preprocessor1D.cpp
 *
 *  Created on: Aug 26, 2011
 *      Author: tpz22rc
 */

#include "Preprocessor1D.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>

using std::ifstream;
using std::cerr;
using std::clog;

Preprocessor1D::Preprocessor1D() {
	numOfC=numOfE=0;
}

Preprocessor1D::~Preprocessor1D() {
}

Preprocessor1D::Preprocessor1D(const int &numOfC,const int &numOfE){
	this->numOfC=numOfC;
	this->numOfE=numOfE;
}

double * Preprocessor1D::readCoord(const char *f){
	double *coord=new double[numOfC+1];
	ifstream in(f);
	if(!in.is_open()){
		std::cerr<<"failed to open: "<<f;
		abort();
	}
	for(int i=1;i<=numOfC;i++)
		in>>coord[i];
	in.close();
	return coord;
}

int ** Preprocessor1D::readElement(const char *f){
	int **connect=new int*[numOfE+1];
	for(int i=0;i<=numOfE;i++)
		connect[i]=new int[3];
	ifstream in(f);
	if(!in.is_open()){
		std::cerr<<"failed to open: "<<f;
		abort();
	}
	for(int i=1;i<=numOfE;i++){
		for(int j=1;j<=2;j++){
			in>>connect[i][j];
		}
	}
	in.close();
	return connect;
}

double * Preprocessor1D::geometry(double *coord,int **connect){
	double y[3];
	double *geo=new double[numOfE+1];
	int cp;
	for(int i=1;i<=numOfE;i++){
		for(int j=1;j<=2;j++){
			cp=connect[i][j];
			y[j]=coord[cp];
		}
		geo[i]=fabs(y[2]-y[1]);
	}
	return geo;
}

double * Preprocessor1D::lmass(double *geo,int **connect){
	double *lm=new double[numOfC+1];
	int i;
	for (i =1;i<=numOfC;i++)
		lm[i]=0.0;
	int ielem, inode, in;
	double rj, rj2;
	for (ielem =1;ielem<=numOfE;ielem++) {
		rj =geo[ielem];
		rj2=rj/2;
		for (inode =1;inode<=2;inode++) {
			in=connect[ielem][inode];
			lm[in]=lm[in]+rj2;
		}
		for (inode=1;inode<=numOfC;inode++) {
			lm[inode]=1.00/lm[inode];
		}

	}
	return lm;
}
